﻿// 7-4 顺序存储的二叉树的最近的公共祖先问题 (25分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
class tree
{
public:
	int flag;
	int inf;
	tree* l;
	tree* r;
	tree* p;
	tree(tree* parent)
	{
		l = NULL;
		p = parent;
		r = NULL;
		inf = -1;
		flag = 0;

	}
	tree()
	{
		l = NULL;
		p = NULL;
		r = NULL;
		inf = -1;
		flag = 0;

	}
	void creat(tree* parent)
	{
		l = new tree(parent);
		r = new tree(parent);
	}
	void creatl(tree* parent)
	{
		l = new tree(this);

	}
};

void op(tree* p)
{
	int u;
	int c[3];
	if (p->l != NULL && p->r == NULL) {
		if (p->l->inf > p->inf) {
			u = p->l->inf;
			p->l->inf = p->inf;
			p->inf = u;
		}
	}
	if (p->l == NULL && p->r != NULL) {
		if (p->r->inf < p->inf) {
			u = p->r->inf;
			p->r->inf = p->inf;
			p->inf = u;
		}

	}
	if (p->l != NULL && p->r != NULL)
	{
		c[0] = p->l->inf;
		c[1] = p->r->inf;
		c[2] = p->inf;
		sort(c, c + 3);
		p->l->inf = c[0];
		p->inf = c[1];
		p->r->inf = c[2];
	}
	else
	{

	}
}
tree* find(tree*p,int g, tree* p1,int&d)
{
	if (g>=d) { p1 = p; }
	else { d++; }
	if (p->inf != -1)
	{
		//if (p->inf == g) { p1 = p; }
		
		if ((p->l != NULL)) { find(p->l, g,p1,d); }

		if ((p->r != NULL)) { find(p->r, g,p1,d); }
	}

	else
	{



		return NULL;
	}
}

void firstread(tree * p, queue<int> & c)
{
	
	if (p->inf != -1)
	{
		p->inf = c.front();
		c.pop();
		if ((p->l != NULL)) { firstread(p->l, c); }
	
		if ((p->r != NULL)) { firstread(p->r, c); }
	}

	else
	{



		return;
	}
}


void fd(tree * p)
{

	if ((p->l == NULL && p->r == NULL))
	{
		cout << p->inf << " ";

	}
	else
	{

		if ((p->l != NULL)) { fd(p->l); }
		cout << p->inf << " ";
		if ((p->r != NULL)) { fd(p->r); }
	}
}

int main()
{
	queue<tree*>a;
	queue<int>c;
	int n, m, i, q,w,d1,d2,j;
	cin >> n;
	queue<tree*>k;
	vector<tree*>f;

	int* b = new int[n];
	for (i = 0; i < n; i++)
	{
		cin >> b[i];
	}


	for (i = 0; i < n; i++)
	{

		c.push(b[i]);
	}

	/*tree* head = new tree;
	a.push(head);
	head->inf = b[0];
	f.push_back ( head);

	for (i = 1; i < n; i = i + 2)
	{

		if (n - i == 1)
		{
			a.front()->creatl(a.front());
			a.front()->l->inf = b[i];
			f.push_back(a.front()->l);
			break;
		}
		else
		{
			a.front()->creat(a.front());
			a.front()->l->inf = b[i];
			a.front()->r->inf = b[i + 1];
			 f.push_back(a.front()->l);
			 f.push_back(a.front()->r);
			a.push(a.front()->l);
			a.push(a.front()->r);
			a.pop();
		}
	}

	cin >> q >> w;//输入查找的两样东西
	q--;
	w--;
	tree* p1 = new tree;
	p1 = f[q ];
	tree* p2 = new tree;
	p2 = f[w];
	/*q--;
	w--;
	//firstread(head, c);
	d1 = 0;
	d2 = 0;

	p1=find(head, q, p1, d1);
	p2=find(head, w, p2, d2);
	cout << p1->p->inf << "p";
	cout << p2->p->inf << "p";*/
	
	j = 0;
	if (b[q] == 0 || b[w] == 0)
	{
		if (b[q] == 0 && b[w] == 0)
		{
			if (q>w)
			{
				cout << "ERROR: T["<<w<<"] is NULL" ;
			}
			else
			{
				cout << "ERROR: T[" << q<< "] is NULL";
			}
		}
		else
		{
			if (b[q]==0)
			{
				cout << "ERROR: T[" << q << "] is NULL";
			}
			else
			{
				cout << "ERROR: T[" << w << "] is NULL";
			}
		}
	}
	else
	{
		while (p1 != p2)
		{
			p1 = p1->p;
			p2 = p2->p;
		}
		cout << p1->inf;
	}*/
}
